﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using PalFramework.Object;
using PalBattleEmulator.Command;

namespace PalBattleEmulator.Graphic
{
    public class EnemyGraphic : UnitGraphic
    {
        private IEnemy enemy;
        private GraphicFrame frame;
        private Queue<int> idleFrames;
        private int idleSpan;
        private int idleCounter;

        public void Initialize(IEnemy enemy)
        {
            this.enemy = enemy;
            this.idleFrames = new Queue<int>();


            //填充source
            sources = new List<BitmapSource>();
            string imageIndex = enemy.Sprite;
            for (int i = 0; ; i++)
            {
                BitmapSource img = (BitmapSource)ContextModules.EnemyImageManager.GetImage(imageIndex, i);
                if (img == null) break;
                sources.Add(img);
            }

            //初始化frame
            idleSpan = enemy.Template.GraphicInfo.IdleSpan;
            idleCounter = 0;
            frame = new GraphicFrame();
            frame.FrameIndex = 0;
            frame.PosX = enemy.Array.PosX;
            frame.PoxY = enemy.Array.PosY + enemy.Template.GraphicInfo.YRevice;
            frame.ZIndex = ZINDEX_NORMAL;

            for (int i = 0; i < enemy.Template.GraphicInfo.IdleFrames; i++)
            {
                idleFrames.Enqueue(i);
            }
            
        }

        public override void OnCommand(GameCommand command)
        {
            throw new NotImplementedException();
        }

        public override GraphicFrame GetIdleFrame()
        {
            if (++idleCounter % idleSpan == 0 || idleSpan == 99)
            {
                idleCounter = 0;
                int frameIndex = idleFrames.Dequeue();
                frame.FrameIndex = frameIndex;
                idleFrames.Enqueue(frameIndex);
            }
            return frame;
        }

    }
}
